﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.VisualDevModelData;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 0代码功能数据表
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2020.10.27
    /// </summary>
    [Route("api/VisualDevelopment/Model/app")]
    public class VisualdevModelDataAppController : BaseApiController
    {
        private VisualDevBll visualDevBll = new VisualDevBll();
        private VisualDevModelDataBll visualDevModelData = new VisualDevModelDataBll();

        #region GET

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="modelId">主键id</param>
        /// <param name="input">分页查询条件</param>
        /// <returns></returns>
        [HttpGet("{modelId}/List")]
        public async Task<ResponseResult> List(string modelId, VisualDevModelListQueryInput input)
        {
            var data = await visualDevBll.GetInfo(modelId);
            var realList = await visualDevModelData.GetListResult(data, input);
            return Success(realList);
        }

        /// <summary>
        /// 获取列表表单配置JSON
        /// </summary>
        /// <param name="modelId">主键id</param>
        /// <returns></returns>
        [HttpGet("{modelId}/Config")]
        public async Task<ResponseResult> GetData(string modelId)
        {
            var data = await visualDevBll.GetInfo(modelId);
            var output = data.MapTo<VisualDevModelDataConfigAppOutput>();
            return Success(output);
        }



        /// <summary>
        /// 获取数据信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        [HttpGet("{modelId}/{id}")]
        public async Task<ResponseResult> GetInfo(string id, string modelId)
        {
            var output = new VisualDevModelDataInfoOutput();
            var visualdevEntity = await visualDevBll.GetInfo(modelId);
            //有表
            if (!string.IsNullOrEmpty(visualdevEntity.Tables) && !"[]".Equals(visualdevEntity.Tables))
            {
                output = await visualDevModelData.GetTableInfo(id, visualdevEntity);
                return Success(output);
            }
            //无表
            VisualDevModelDataEntity entity = await visualDevModelData.GetInfo(id);
            string data = await visualDevModelData.autoFeilds(visualdevEntity, entity.Data);
            entity.Data = data;
            output = entity.MapTo<VisualDevModelDataInfoOutput>();
            return Success(output);
        }

        #endregion

        #region POST

        /// <summary>
        /// 创建数据
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="visualdevModelDataCrForm"></param>
        /// <returns></returns>
        [HttpPost("{modelId}")]
        public async Task<ResponseResult> Create(string modelId, [FromBody] VisualDevModelDataCrForm visualdevModelDataCrForm)
        {
            VisualDevEntity visualdevEntity = await visualDevBll.GetInfo(modelId);
            await visualDevModelData.Create(visualdevEntity, visualdevModelDataCrForm);
            return Success("新建成功");
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="modelId"></param>
        /// <param name="visualdevModelDataUpForm"></param>
        /// <returns></returns>
        [HttpPut("{modelId}/{id}")]
        public async Task<ResponseResult> Update(string modelId, string id, [FromBody] VisualDevModelDataCrForm visualdevModelDataUpForm)
        {
            VisualDevEntity visualdevEntity = await visualDevBll.GetInfo(modelId);
            var isOk = await visualDevModelData.Update(id, visualdevEntity, visualdevModelDataUpForm);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="modelId"></param>
        /// <returns></returns>
        [HttpDelete("{modelId}/{id}")]
        public async Task<ResponseResult> Delete(string id, string modelId)
        {
            VisualDevEntity visualdevEntity = await visualDevBll.GetInfo(modelId);
            if (!string.IsNullOrEmpty(visualdevEntity.Tables) && !"[]".Equals(visualdevEntity.Tables))
            {
                var isOk = await visualDevModelData.TableDelete(id, visualdevEntity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            VisualDevModelDataEntity entity = await visualDevModelData.GetInfo(id);
            if (entity != null)
            {
                var isOk = await visualDevModelData.Delete(entity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            return Error("删除失败");
        }
        #endregion
    }
}
